local joy__qixian = fk.CreateSkill {

  name = "joy__qixian",

  tags = { Skill.Compulsory, },

}



joy__qixian:addEffect(fk.EventPhaseEnd, {
  name = "joy__qixian",
  anim_type = "control",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(joy__qixian.name) and player.phase == Player.Play and not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local card = player.room:askForCard(player, 1, 1, false, joy__qixian.name, true, ".", "#joy__qixian-card")
    if #card > 0 then
      event:setCostData(self, card[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addToPile(joy__qixian.name, event:getCostData(self), false, joy__qixian.name)
  end,
})

joy__qixian:addEffect("maxcards", {
  name = "#joy__qixian_maxcards",
  frequency = Skill.Compulsory,
  fixed_func = function (self, player)
    if player:hasSkill(joy__qixian.name) then
      return 7
    end
  end,
})

joy__qixian:addEffect(fk.TurnEnd, {
  name = "#joy__qixian_delay",
  mute = true,
  events = {fk.TurnEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and #player:getPile("joy__qixian") > 0
  end,
  on_use = function(self, event, target, player, data)
    local dummy = Fk:cloneCard("slash")
    dummy:addSubcards(player:getPile("joy__qixian"))
    player.room:obtainCard(player, dummy, false, fk.ReasonPrey)
  end,
})

return joy__qixian